// Package models
// @Title  menuModel.go
// @Description  菜单模型
// @Author  小斌 (时间 格式是2021/12/02  10:20)
// @Update  小斌 (时间 格式是2021/12/02  10:20)

package models

import (
	"github.com/astaxie/beego/orm"
	"time"
)

// MenuObj 用户角色 实体类
type MenuObj struct {
	Id                 int    `valid:"id"`
	Type               string `valid:"type"`
	Title               string `valid:"title"`
	PId                int `valid:"p_id"`
	Icon                string `valid:"icon"`
	Href                string `valid:"href"`
	Target                string `valid:"target"`
	Sort					int `valid:"sort"`
	ControllerName                string `valid:"controller_name"`
	ActionName                string `valid:"action_name"`
	IsDelete				int `valid:"is_delete"`
	UpdateTime                time.Time `valid:"update_time" orm:"auto_now"`
	AddTime                time.Time `valid:"add_time" orm:"auto_now_add"`

}

func (this *MenuObj) TableName() string {
	return "menu"
}

func init() {
	orm.RegisterModel(new(MenuObj))
}

// GetMenuList 获取分页数据
func GetMenuList(pageSize, page int,menuType string) (*[]MenuObj, int, error) {
	var (
		list []MenuObj
		err  error
	)

	qs := orm.NewOrm().QueryTable(new(MenuObj)).OrderBy("-sort")
	if menuType != "" {
		qs = qs.Filter("type", menuType)
	}
	_, err = qs.Filter("is_delete",0).Limit(pageSize).Offset((page - 1) * pageSize).RelatedSel().All(&list)
	count, _ := qs.Count()
	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return nil, 0, err
	}

	return &list, int(count), nil
}

// GetMenuListByIds 根据多个id获取记录
func GetMenuListByIds(idArr []int,menuType string) (*[]MenuObj,error) {
	var (
		list []MenuObj
		err  error
	)
	qs := orm.NewOrm().QueryTable(new(MenuObj)).OrderBy("-sort")
	if menuType != "" {
		qs = qs.Filter("type", menuType)
	}
	_,err = qs.Filter("id__in", idArr).Filter("is_delete", 0).All(&list)

	if err == orm.ErrNoRows || err == orm.ErrMissPK {
		return &list,nil
	}

	return &list,nil
}
